﻿Imports System.Data.Linq
Public Class CTonNganSachController
    Dim objDataContext As QLyNganSachDataContext
    Public Sub New()
        objDataContext = New QLyNganSachDataContext
    End Sub
    Public Sub New(ByVal _obj As QLyNganSachDataContext)
        objDataContext = _obj
    End Sub
    Public Function SelectAllRecord(ByVal idDonVi As Integer) As ISingleResult(Of sp_GetPhanBoNganSachNamResult)
        Try
            'Return (From t In objDataContext.sp_GetPhanBoNganSachNam(idDonVi, Date.Now.Year)
            '            Select New CTonNganSachInfo With {
            '                                              .ID_DanhMuc = t.ID_DanhMuc,
            '                                              .Ma = t.Ma,
            '                                              .TuChi = If(t.TuChi.HasValue, t.TuChi, 0),
            '                                              .HienVat = If(t.HienVat.HasValue, t.HienVat, 0),
            '                                              .BVCanDoi = If(t.BVCanDoi.HasValue, t.BVCanDoi, 0),
            '                                              .Khac = If(t.Khac.HasValue, t.Khac, 0),
            '                                              .TongCong = .Khac + .HienVat + .BVCanDoi + .TuChi})
            Return objDataContext.sp_GetPhanBoNganSachNam(idDonVi, Date.Now.Year)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Sub InsertRecord(ByVal obj As CTonNganSachInfo, ByVal idDonVi As Integer)
        Try
            Dim LocalTable As New tblTonDanhMuc
            If (obj.TuChi <> 0) Then
                With LocalTable
                    .ID_DanhMuc = obj.ID_DanhMuc
                    .ID_DonVi = idDonVi
                    .ID_LoaiNganSach = 1
                    .Nam = Date.Now.Year
                    .SoTien = obj.TuChi
                End With
                objDataContext.tblTonDanhMucs.InsertOnSubmit(LocalTable)
            End If
            
            If (obj.HienVat <> 0) Then
                LocalTable = New tblTonDanhMuc
                With LocalTable
                    .ID_DanhMuc = obj.ID_DanhMuc
                    .ID_DonVi = idDonVi
                    .ID_LoaiNganSach = 2
                    .Nam = Date.Now.Year
                    .SoTien = obj.HienVat
                End With
                objDataContext.tblTonDanhMucs.InsertOnSubmit(LocalTable)
            End If
            
            If (obj.BVCanDoi <> 0) Then
                LocalTable = New tblTonDanhMuc
                With LocalTable
                    .ID_DanhMuc = obj.ID_DanhMuc
                    .ID_DonVi = idDonVi
                    .ID_LoaiNganSach = 3
                    .Nam = Date.Now.Year
                    .SoTien = obj.BVCanDoi
                End With
                objDataContext.tblTonDanhMucs.InsertOnSubmit(LocalTable)
            End If
            
            If (obj.Khac <> 0) Then
                LocalTable = New tblTonDanhMuc
                With LocalTable
                    .ID_DanhMuc = obj.ID_DanhMuc
                    .ID_DonVi = idDonVi
                    .ID_LoaiNganSach = 4
                    .Nam = Date.Now.Year
                    .SoTien = obj.Khac
                End With
                objDataContext.tblTonDanhMucs.InsertOnSubmit(LocalTable)
            End If

            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub UpdateRecord(ByVal obj As CTonNganSachInfo, ByVal idDonVi As Integer)
        Try
            DeleteRecord(obj.ID_DanhMuc, idDonVi)
            InsertRecord(obj, idDonVi)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub DeleteRecord(ByVal _idDanhMuc As Integer, ByVal idDonVi As Integer)
        Try
            objDataContext.tblTonDanhMucs.DeleteAllOnSubmit( _
              (From r In objDataContext.tblTonDanhMucs _
               Where r.ID_DonVi = idDonVi And r.ID_DanhMuc = _idDanhMuc))
            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Function CheckIfExists(ByVal _id As String, ByVal _ten As String) As Boolean
        Return objDataContext.tblDonVis.Any(Function(d) d.Ten = _ten And d.ID_DonVi <> _id)
    End Function
End Class
